package com.ruoyi.system.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.easysdk.factory.Factory;

import com.ruoyi.common.core.utils.RandomUtils;
import com.ruoyi.system.config.AlipayTemplate;
import com.ruoyi.system.domain.CwUserOrder;
import com.ruoyi.system.service.impl.CwShopUserServiceImpl;
import com.ruoyi.system.service.impl.CwUserOrderServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * 支付宝接口
 */
@RestController
@RequestMapping("/alipay")
public class AliPayController {

    @Resource
    AlipayTemplate alipayTemplate;

    @Resource
    CwUserOrderServiceImpl cwUserOrderService;

    @GetMapping(value = "/pay", produces = "text/html")
    @ResponseBody
    public String pay(@RequestParam long userId,@RequestParam Double totalAmount,@RequestParam String orderName,@RequestParam String orderImg) throws AlipayApiException {
        CwUserOrder order = new CwUserOrder();
        order.setOrderId(Long.valueOf(RandomUtils.generateByTimestamp()));
        order.setOrderName(orderName);
        order.setOrderImg(orderImg);
        order.setUserId(userId);
        order.setInterfaceInfoId(294389472934L);
        order.setTotalAmount(totalAmount);
        order.setPaymentMethod("支付宝");
        System.out.println("order"+order);
        cwUserOrderService.insertCwUserOrder(order);
        return alipayTemplate.pay(order);
    }
//    @GetMapping(value = "/pay", produces = "text/html")
//    @ResponseBody
//    public String pay(CwUserOrder order) throws AlipayApiException {
//        order.setUserId(129904058947L);
//        order.setInterfaceInfoId(294389472934L);
//        order.setPaymentMethod("支付宝");
//        cwUserOrderService.insertCwUserOrder(order);
//        return alipayTemplate.pay(order);
//    }

    @PostMapping("/notify")  // 注意这里必须是POST接口
    public String payNotify(HttpServletRequest request) throws Exception {
        if (request.getParameter("trade_status").equals("TRADE_SUCCESS")) {
            System.out.println("=========支付宝异步回调========");

            Map<String, String> params = new HashMap<>();
            Map<String, String[]> requestParams = request.getParameterMap();
            for (String name : requestParams.keySet()) {
                params.put(name, request.getParameter(name));
                // System.out.println(name + " = " + request.getParameter(name));
            }

            String tradeNo = params.get("out_trade_no");
            String gmtPayment = params.get("gmt_payment");
            String alipayTradeNo = params.get("trade_no");
            // 支付宝验签
            if (Factory.Payment.Common().verifyNotify(params)) {
                // 验签通过
                System.out.println("交易名称: " + params.get("subject"));
                System.out.println("交易状态: " + params.get("trade_status"));
                System.out.println("支付宝交易凭证号: " + params.get("trade_no"));
                System.out.println("商户订单号: " + params.get("out_trade_no"));
                System.out.println("交易金额: " + params.get("total_amount"));
                System.out.println("买家在支付宝唯一id: " + params.get("buyer_id"));
                System.out.println("买家付款时间: " + params.get("gmt_payment"));
                System.out.println("买家付款金额: " + params.get("buyer_pay_amount"));
                // 更新订单状态
            }
        }
        return "success";
    }
}

